//
//  NSDate+Compare.m
//  CToolBox
//
//  Created by crj on 16/8/9.
//  Copyright © 2016年 oyf. All rights reserved.
//

#import "NSDate+Compare.h"

@implementation NSDate (Compare)

+ (NSDate *)getLastDateWithDate:(NSDate *)date afterDays:(long)afterDays{
    
    NSTimeInterval oneDay = 24*60*60;//1天的秒数
    NSDate *lastDate = [NSDate dateWithTimeInterval:afterDays*oneDay sinceDate:date];

    
    return lastDate;
}

+(NSInteger)getDaysFrom:(NSDate *)serverDate To:(NSDate *)endDate
{
    NSCalendar *gregorian = [[NSCalendar alloc]
                             initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    
    [gregorian setFirstWeekday:2];
    
    //去掉时分秒信息
    NSDate *fromDate;
    NSDate *toDate;
    [gregorian rangeOfUnit:NSCalendarUnitDay startDate:&fromDate interval:NULL forDate:serverDate];
    [gregorian rangeOfUnit:NSCalendarUnitDay startDate:&toDate interval:NULL forDate:endDate];
    NSDateComponents *dayComponents = [gregorian components:NSCalendarUnitDay fromDate:fromDate toDate:toDate options:0];
    
    return dayComponents.day;
}

+ (BOOL)isToday:(NSDate *)lastDate{
    
    return [self isSameDayWithOldDate:lastDate newDate:[NSDate date]];
}

+ (BOOL)isSameDayWithOldDate:(NSDate *)oldDate newDate:(NSDate *)newDate{
    
    BOOL isSameDate = NO;
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"YYYY/MM/dd"];
    if(oldDate && newDate){
        NSString *oldDateStr = [dateFormatter stringFromDate:oldDate];
        NSString *newDateStr = [dateFormatter stringFromDate:newDate];
        isSameDate = [oldDateStr isEqualToString:newDateStr];
        
    }
    return isSameDate;
}

+ (BOOL)timeOfArrivalToDate:(NSDate *)date{
    
    NSDate *now = [NSDate date];
    NSDate *earlier_date = [now earlierDate:date];
    if([earlier_date isEqual:date]){
        return YES;
    }else{
        return NO;
    }
}

- (NSString *)countdown{
   
    
    NSTimeInterval secondsInterval= [self timeIntervalSinceDate:[NSDate date]];
    long h = (long)secondsInterval/(60*60);
    long m = (long)secondsInterval/60%60;
    long s = (long)secondsInterval%60;
    NSString *hStr = h<10?[NSString stringWithFormat:@"0%ld",h]:[NSString stringWithFormat:@"%ld",h];
    NSString *mStr = m<10?[NSString stringWithFormat:@"0%ld",m]:[NSString stringWithFormat:@"%ld",m];
    NSString *sStr = s<10?[NSString stringWithFormat:@"0%ld",s]:[NSString stringWithFormat:@"%ld",s];
    
    return [NSString stringWithFormat:@"%@:%@:%@",hStr,mStr,sStr];
}

+ (NSDate *)dateFromeString:(NSString *)string{
    
    string = [string stringByReplacingOccurrencesOfString:@"T" withString:@" "];
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    
    NSDate *date = [formatter dateFromString:string];
    return date;
}

+ (NSString *)stringFromeDate:(NSDate *)date{
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSString *dateString = [formatter stringFromDate:date];
    return dateString;
}

+ (NSString *)stringDateFromeDate:(NSDate *)date{
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy.M.d"];
    NSString *dateString = [formatter stringFromDate:date];
    return dateString;
}

+ (NSString *)stringFromeDate:(NSDate *)date dateFormat:(NSString *)dateFormat{
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:dateFormat];
    
    NSString *string = [formatter stringFromDate:date];
    return string;
}

+ (NSDate *)dateFromeString:(NSString *)string dateFormat:(NSString *)dateFormat{
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:dateFormat];
    
    NSDate *date = [formatter dateFromString:string];
    return date;
}

+ (BOOL)isWorkTime{
    
    int week = [NSDate getWeekdayFromDate:[NSDate date]];
    
    if(week<=1 || week>=7){//周末
        return NO;
    }
    
    int hour = [NSDate getHourFromDate:[NSDate date]];
    
    if(hour<9 || hour>17){//9:00-17:00之外
        return NO;
    }
    
    return YES;
}


+ (int)getWeekdayFromDate:(NSDate*)date
{
    NSCalendar* calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    calendar.timeZone = [NSTimeZone systemTimeZone];
    NSDateComponents* components = [[NSDateComponents alloc] init] ;
    NSInteger unitFlags = NSCalendarUnitWeekday;
    components = [calendar components:unitFlags fromDate:date];
    NSInteger weekday = [components weekday];
    return (int)weekday;
}

+ (int)getHourFromDate:(NSDate*)date
{
    NSCalendar* calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    calendar.timeZone = [NSTimeZone systemTimeZone];
    NSDateComponents* components = [[NSDateComponents alloc] init] ;
    NSInteger unitFlags = NSCalendarUnitHour;
    components = [calendar components:unitFlags fromDate:date];
    NSInteger hour = [components hour];
    return (int)hour;
}

//生成一个date对象
+ (NSDate *)dateWithYear:(int)year month:(int)month day:(int)day{
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyy-MM-dd"];
    formatter.timeZone = [NSTimeZone systemTimeZone];
    
    NSDate *date = [formatter dateFromString:[NSString stringWithFormat:@"%d-%d-%d",year,month,day]];
    return date;
}

//生成一个当年当月的date对象
+ (NSDate *)dateWithDay:(int)day
{
    NSDate *dateNow = [NSDate date];
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy"];
    NSInteger currentYear = [[formatter stringFromDate:dateNow] integerValue];
    [formatter setDateFormat:@"MM"];
    NSInteger currentMonth = [[formatter stringFromDate:dateNow]integerValue];
    
    [formatter setDateFormat:@"yyy-MM-dd"];
    formatter.timeZone = [NSTimeZone systemTimeZone];
    NSDate *date = [formatter dateFromString:[NSString stringWithFormat:@"%zd-%zd-%d",currentYear, currentMonth,day]];
    return date;
}

@end
